<template>
  <!-- <div class='container'>目录</div> -->
  <div class="container">
    <el-card>
      <el-breadcrumb
        separator-class="el-icon-arrow-right"
        slot="header"
        v-if="subjectName"
      >
        <el-breadcrumb-item>学科管理</el-breadcrumb-item>
        <el-breadcrumb-item>{{ subjectName }}</el-breadcrumb-item>
        <el-breadcrumb-item>目录管理</el-breadcrumb-item>
      </el-breadcrumb>
      <div class="filter-container">
        <el-form>
          <div class="filter-container" style="padding: 10px">
            <el-row type="flex">
              <el-col :span="6">
                <el-form-item label="学科目录">
                  <el-input
                    v-model="page.directoryName"
                    style="width: 180px"
                    type="text"
                    placeholder="请输入目录"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="18">
                <el-form-item label="状态">
                  <el-select
                    v-model="page.state"
                    style="width: 180px"
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="item in reuiStatus"
                      :key="item.id"
                      :value="item.id"
                      :label="item.value"
                    />
                  </el-select>
                  <el-button
                    size="small"
                    type="defult"
                    style="margin-left: 10px"
                    @click="clertxt"
                    >清除</el-button
                  >
                  <el-button type="primary" size="small" @click="mlsearch"
                    >搜索</el-button
                  >
                  <el-button
                    class="filter-item fr"
                    type="success"
                    size="small"
                    icon="el-icon-edit"
                    @click="addml()"
                    >新增目录</el-button
                  >
                  <el-button
                    class="filter-item fr"
                    v-if="jectname"
                    type="text"
                    icon="el-icon-back"
                    @click="toject"
                    >返回学科</el-button
                  >
                </el-form-item>
              </el-col>
            </el-row>
          </div>
        </el-form>
      </div>
      <!-- 数据记录项 -->
      <el-alert
        v-if="alertText !== ''"
        :title="alertText"
        type="info"
        class="alert"
        :closable="false"
        show-icon
      />
      <!-- 表格 -->
      <div class="el-table__header">
        <el-table
          v-loading="loading"
          element-loading-text="努力加载中..."
          element-loading-spinner="el-icon-loading"
          label-width="100%"
          fit
          :data="MLlist"
        >
          <el-table-column label="序号" type="index"> </el-table-column>
          <el-table-column label="所属学科" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.subjectName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="目录名称" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.directoryName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="创建者" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.username }}</span>
            </template>
          </el-table-column>
          <el-table-column label="创建日期" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.addDate | parseTime }}</span>
            </template>
          </el-table-column>
          <el-table-column label="面试题数量" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.totals }}</span>
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center">
            <template slot-scope="{ row }">
              <span>{{ row.state === 0 ? "已禁用" : "已启用" }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="240" align="center">
            <template slot-scope="{ row }">
              <el-button type="text" @click="mlstop(row)">{{
                row.state === 0 ? "启用" : "禁用"
              }}</el-button>
              <el-button
                type="text"
                :disabled="row.state === 1 ? true : false"
                @click="addml(row)"
                >修改</el-button
              >
              <el-button
                type="text"
                :disabled="row.state === 1 ? true : false"
                @click="mldelrow(row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          background
          :current-page="page.page"
          :total="page.total"
          :page-sizes="[10, 20, 30]"
          @current-change="MLpage"
          layout=" prev, pager, next, sizes, jumper"
        />
      </div>
    </el-card>
    <!-- 放置弹层 -->
    <!-- 添加目录弹层 -->
    <el-dialog
      :title="this.MLform.id ? '修改目录' : '添加目录'"
      :visible="mlshow"
      width="400px"
      @close="btnmlCancel"
    >
      <el-form ref="mlform" :model="MLform" :rules="rules">
        <el-form-item label="所属学科" prop="subjectID" v-show="!this.subjectID">
          <el-select v-model="MLform.subjectID" style="width: 60%">
            <el-option
              v-for="item in simpleml"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="directoryName">
          <el-input
            v-model="MLform.directoryName"
            style="width: 60%"
          ></el-input>
        </el-form-item>
      </el-form>
      <!-- footer -->
      <template solt="footer">
        <el-row type="flex" justify="end" style="padding-top: 10px">
          <el-col :span="12">
            <el-button size="small" @click="btnmlCancel">取消</el-button>
            <el-button type="primary" size="small" @click="btnmlok"
              >确定</el-button
            >
          </el-col>
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { list, add, remove, update, changeState } from '@/api/hmmm/directorys'
import { simple } from '@/api/hmmm/subjects'
export default {
  name: 'dire',
  data () {
    return {
      subjectID: this.$route.query.id,
      subjectName: this.$route.query.name,
      loading: false, // 加载
      jectname: false, // 学科隐藏
      mlshow: false, // 目录新增弹层显示
      alertText: '', // 数据记录
      MLlist: [], // 总目录数据
      state: '', // 启用 禁止
      page: {
        page: 1,
        size: 10,
        total: 0
      },
      MLform: {
        id: null,
        subjectID: '',
        directoryName: '' // 目录名称
      },
      rules: {
        subjectID: [
          { required: true, message: '请输入学科名称', trigger: 'change' }
        ],
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ]
      },
      simpleml: [],
      // 学科option 调用
      reuiStatus: [
        {
          id: '1',
          value: '启用'
        },
        {
          id: '0',
          value: '禁用'
        }
      ]
    }
  },
  created () {
    this.list()
    this.simple()
  },
  mounted () {
    this.showjectname() // 学科隐藏
  },
  methods: {
    // 学科隐藏
    showjectname () {
      if (this.$route.query.id) {
        this.jectname = true
      }
    },
    // 学科简单列表
    async simple () {
      const { data } = await simple()
      this.simpleml = data
    },
    async list () {
      this.loading = true
      // 搜索出现指定结果
      if (this.$route.query.id) {
        this.page.subjectID = this.$route.query.id
      }
      const { data } = await list(this.page)
      this.MLlist = data.items
      this.page.total = data.counts
      this.alertText = `共有${this.page.total}条数据`
      this.loading = false
    },
    // 返回学科
    toject () {
      this.$router.back()
    },
    // 目录分页
    MLpage (val) {
      this.page.page = val
      this.list()
    },
    // 禁止按钮
    async mlstop (row) {
      // console.log(row.state)
      // 调用state 判断
      if (row.state === 0) {
        await changeState({ id: row.id, state: 1 })
      } else {
        await changeState({ id: row.id, state: 0 })
      }
      this.$message.success('操作成功')
      this.list()
    },
    // 清除txt
    clertxt () {
      this.state = ''
      this.page = {
        page: 1, // 页码
        size: 10, // 每页条数
        total: 0 // 总数
      }
      this.list()
    },
    // 搜索按钮
    mlsearch (row) {
      this.list()
    },
    // 删除按钮
    async mldelrow (row) {
      await this.$confirm('确定删除此目录' + ', 是否继续?', '系统提示', {
        type: 'warning'
      })
      await remove(row)
      this.$message.success('删除成功')
      this.list() // 重新加载
    },
    // 修改目录
    async addml (data) {
      this.mlshow = true
      if (data) {
        // 有id 编辑
        this.MLform.id = data.id
        // console.log(this.MLform.subjectID)
        this.MLform.subjectID = data.subjectID
        this.MLform.directoryName = data.directoryName
      } else if (this.subjectID) {
        // 没有 新增
        this.MLform.subjectID = parseInt(this.subjectID)
      }
      // this.mlshow = false
    },
    // 弹层确定按钮
    btnmlok () {
      // 校验表单
      this.$refs.mlform.validate(async (isok) => {
        if (isok) {
          if (this.MLform.id) {
            await update(this.MLform)
            this.$message.success('修改成功')
            this.list()
            this.mlshow = false
          } else {
            await add(this.MLform)
            this.$message.success('添加成功')
            this.list()
            this.mlshow = false
          }
        }
      })
    },
    // 弹层取消按钮
    async btnmlCancel () {
      this.MLform = {
        subjectID: '', // 目录学科
        directoryName: '' // 目录名称
      }
      // 表单重置校验
      await this.$refs.mlform.resetFields()
      this.mlshow = false
    }
  }
}
</script>

<style scoped lang='scss'>
.el-table__header {
  margin-top: 20px;
  width: 100%;
  background-color: #ccc;
}
.el-table .defult-row {
  background: #ccc;
}
.pagination {
  margin-top: 10px;
  float: right;
}
</style>
